var currentJb="";
var bgbmid="",bgbmmc="";
$(document).ready(function () {
	$("#functionNav").attr("href",navAddr);
	//填充左侧保管部门
	var bgbmJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInJb.do",{'tableName':'jb_bgbm'});
	loadLeftList("#kfSelBm2",bgbmJson.rows,'id','name');	
	currentJb = "kfjb";		
	//获取保管部门rows中的第一个Id
	bgbmid=bgbmJson.rows[0]["id"];		
	bgbmmc=bgbmJson.rows[0]["name"];
	//生成默认的列表
	createTableWithOutPage('#jb_kfjb','/antiques/sysmanage.getBmKfBgyInfo.do','id',kfbgyColumns,{"bgbmid":bgbmid});
	
	//点击不同的标签所生成的列表
	$('#kfSelBm2 li').click(function(){
		bgbmid=$(this).attr("value");	
		bgbmmc=$(this).text();	
		var params =new Object();
		params["bgbmid"] = bgbmid;
	 	$('#jb_kfjb').bootstrapTable('refresh',{query:params});
	})
	
	//加载保管员列表
	$("#selBgy_btn").click(function(){	
		var bgyJson = ajaxResult("get",false,ProjectName+"/root.getUserListByOrg.do",{'orgid':bgbmid});
		loadCheckBoxList("#bgyList",bgyJson.rows,'userid','username');
	});
	
	//确定选择的保管员
	$("#btn_add_bgy").click(function(){	
		var selBgyNames = [],selBgyids = [];
		$("#selectBgyModal input[type=checkbox]:checked").each(function(index,content){
			selBgyNames.push( $(this).next().text());
			selBgyids.push($(this).val());
		})	 
		$("#bgyNames").html(selBgyNames.toString());
		$("#fzbgymc").val(selBgyNames.toString());
		$("#fzbgyid").val(selBgyids.toString());
		$('#selectBgyModal').modal('hide');
	});
	
	//打开库房增加窗口
	$("#openKfjb_btn").click(function(){
		var bgyJson = ajaxResult("get",false,ProjectName+"/root.getUserListByOrg.do",{'orgid':bgbmid});
		loadSelect("select[name=zrbgyid]",bgyJson.rows,'userid','username');
		$('#form_kfjb')[0].reset();
		$('#form_kfjb input[type=hidden]').val("");		
		$("#form_kfjb input[name=bgbmid]").val(bgbmid);
		$("#form_kfjb input[name=bgbmmc]").val(bgbmmc);
		$("#form_kfjb #bgyNames").html("");
		$("#btn_add_kfjb").text("增加");
	});
	
//---------------------------------增加/修改库房--------------------------------------------
	$("#btn_add_kfjb").click(function(){
		if(checkform("#form_kfjb")){
			var params =$('#form_kfjb').serializeObject();
			params["zrbgymc"] = $("#form_kfjb select[name=zrbgyid]").find("option:selected").text();
		    if($("#form_kfjb input[name=id]").val()==null || $("#form_kfjb input[name=id]").val()==''){//新增		
		    	delete params["id"];
			    result = ajaxResult("post",false,ProjectName+"/sysmanage.addKfwhTable.do",params);
			    if(result["status"] == 'ok'){
				   alertFunc("增加库房信息成功！");		
				   $('#kfjbModal').modal('hide');
				   $('#jb_kfjb').bootstrapTable('refresh',{});
			   }else{
				   alertFunc(result["message"]);
			   }	
	  	   }else{//修改
			    result = ajaxResult("post",false,ProjectName+"/sysmanage.updateKfwhTable.do",params);
			    if(result["status"] == 'ok'){
				   alertFunc("修改库房信息成功！");		
				   $('#kfjbModal').modal('hide');
				   var params_tbl =new Object();
				   params_tbl["bgbmid"] = bgbmid;
				   $('#jb_kfjb').bootstrapTable('refresh',{query:params_tbl});
			    }else{
				   alertFunc(result["message"]);
			    }	
	  	  }
		}
	});
})

//加载保管员复选框List
function loadCheckBoxList(dom,data,value,name){
	var result="";
	if(data!=null || data!=""){
		$.each(data,function(index,content){
			result +="<input type='checkbox' value='"+content[value]+"'><span>"+content[name]+"</span><br/>";
		})
	}
	$(dom).html(result);
}

//加载库房维护左侧的导航栏
function loadLeftList(dom,data,value,name){
	var result="";
	if(data!=null || data!=""){
		$.each(data,function(index,content){
			if(index==0){
				result +="<li class='active' role='presentation' value='"+content[value]+"'><a href='#tab_cplb' role='tab' data-toggle='tab' value='"+content[value]+"'>"+content[name]+"</a></li><br/>";
			}else{
				result +="<li role='presentation' value='"+content[value]+"'><a href='#tab_cplb' role='tab' data-toggle='tab' value='"+content[value]+"'>"+content[name]+"</a></li><br/>";
			}
		})
	}
	$(dom).html(result);
}

//点击修改按钮，填充修改表单
function modal_modi(data){
	var bgyJson = ajaxResult("get",false,ProjectName+"/root.getUserListByOrg.do",{'orgid':bgbmid});
	loadSelect("select[name=zrbgyid]",bgyJson.rows,'userid','username');
	var tabname = currentJb;
	loadData("#form_"+tabname,data);		
	$("#form_"+tabname+" input[name=bgbmid]").val(bgbmid);
	$("#form_"+tabname+" input[name=bgbmmc]").val(bgbmmc);
	$("#form_"+tabname+" #bgyNames").html(data["fzbgymc"]);
	$("#btn_add_"+tabname).text("修改");
	$('#'+tabname+'Modal').modal('show');
}


//删除记录
function del_jb(id){		
	var params = new Object();
	params["id"] = id;
	//params["tableName"] = currentJb;	
	if(currentJb=="kfjb"){
		delRecord("#jb_"+currentJb,"/sysmanage.deleteKfwhTable.do",params);
	}
	else{
		delRecord("#jb_"+currentJb,"/sysmanage.deleteKfbglb.do",params);	
	}	

}

/* 库房保管员对应关系表表头开始  */
var kfbgyColumns=[{
	 field: 'check',
	 checkbox: true
    },{
     field: 'kfmc',
	 title: '库房名称'
    },{
     field: 'zrbgymc',
   	 title: '责任保管员名称'
    },{
        field: 'fzbgymc',
      	 title: '辅助保管员名称'
       },{
        field: 'sorted',
      	 title: '排序编号'
       },{
     field: 'operate',
     title: '操作',
     formatter : operateFormat
    }];

//表格操作列
function operateFormat(value, row, index) {  	
  	return "<a onclick='modal_modi("+JSON.stringify(row)+")' href='javascript:void(0)'>修改</a>&nbsp;&nbsp;&nbsp;<a onclick='del_jb("+row.id+")' href='javascript:void(0)'>删除</a>";
} 
